<template>
    <div  class="address">
        <div v-for="( item, index ) in list" :key="index" class="address-item">
            <div class="address-item-b boder-b">
                <div class="address-item-bh">
                    <span v-if="item.is_default" class="tag-default" >[默认]</span>
                    <span class="name">{{ item.contact_name }}</span>
                    <span class="tel" >{{ item.contact_tel }}</span>
                </div>
                <div class="address-item-bf">
                    {{ item.area }} {{ item.address }}
                    <a v-if="select" @click="selectAddress(item)" href="javascript:;" class="weui-btn weui-btn_mini weui-btn_primary address-use">使用</a>
                </div>
            </div>
            <div class="address-item-f boder-b">
                <router-link v-if="select" :to="{ name: 'addressEdit', params: { id: item.id, select: 'select' } }" class="address-item-fa boder-r">
                    编辑
                </router-link>

                <router-link v-if="!select" :to="{ name: 'addressEdit', params: { id: item.id } }" class="address-item-fa boder-r">
                    编辑
                </router-link>
                
                <div @click="setDefault(item) " class="address-item-fa">
                    设置默认
                </div>
            </div>
        </div>

        <router-link v-if="select" class="bottom-bar" to="/addressCreate/select">
            添加新收货地址
        </router-link>

        <router-link v-if="!select" class="bottom-bar" to="/addressCreate">
            添加新收货地址
        </router-link>
    </div>

</template>

<script>
    import Cookies from 'js-cookie'
    export default {
        data() {
            return {
                pid: 0,
                list: [],
                select: this.$route.params.select === 'select'
            }
        },
        created() {
            let _this = this
            this.pid = Cookies.get('ORDER-PID')
            this.$nextTick(function(){
                window.addEventListener("popstate", function(e) {
                    if (_this.select) {
                        _this.$router.push({ name: 'orderCreate', params: { pid: _this.pid }});
                    } else {
                        window.location.href = '/member';
                    }
                }, false); 
                var state = { 
                    title: "title", 
                    url: "#/address/select"
                }; 
                window.history.pushState(state, "title", "#/address/select");
            });
            this.getAddress()
        },
        methods: {
            // 选择地址
            selectAddress(item) {
                Cookies.set('SET-ADDRESS', item.id)
                this.$router.push({ name: 'orderCreate', params: { pid: this.pid }});
            },
            // 获取地址
            getAddress() {
                axios.get('/address').then(res => {
                    res = res.data
                    if (res.status === 'success') {
                        this.list = res.data
                    }
                }).catch(err => {

                })
            },
            // 设置默认
            setDefault(address) {
                let loading = weui.loading('请稍等...');
                axios.post('/address/setDefault', {
                    id: address.id
                }).then(res => {
                    loading.hide();
                    res = res.data
                    if (res.status === 'success') {
                        this.getAddress()
                    }
                }).catch(err => {
                    console.log(err)
                } )
            },
            // 编辑
            edit() {   
            }
        }
    }
</script>

<style lang="scss" scoped>
    a.address-item-fa {
        color: #333;
    }
</style>
